<?php

/**
 * Hooks of Form Helper helps create full validate client data.
 *
 * PHP versions 5
 * CAKEPHP versions 2.x
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       System.Validate
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppHelper', 'View/Helper');

class ValidateHelper extends AppHelper {

    /**
     * Validate maps
     *
     * @var array
     */
    protected $_maps = array();

    /**
     * Returns a list of events this object is implementing, when the class is registered
     * in an event manager, each individual method will be associated to the respective event.
     *
     * @return array associative array or event key names pointing to the function
     * that should be called in the object when the respective event is fired
     */
    public function implementedEvents() {
        return array(
            'HookForm.create' => 'create',
            'HookForm.end' => 'end',
            'HookForm.input' => 'input'
        );
    }

    /**
     * Hook Form create
     * 
     * @param string $model
     * @param array $options
     * @return voild
     */
    public function create($model = null, $options = array()) {
        
    }

    /**
     * Hook Form end
     * 
     * @param mixed $options
     * @return voild
     * @see FormHelper::end
     */
    public function end($options = null) {
        //pr($this->_maps);
        $Model = $this->_View->Form->getModel();
        if (!$this->_maps || (!$Model && !array_filter($this->_maps))) {
            return;
        }
        foreach ($this->_maps as $field => $rules) {
            $rules = (array) $rules;
        }
        //pr('---------------');
        echo '<pre>Hook form end</pre>';
        $this->_maps = array();
    }

    /**
     * Hook Form input
     *
     * @param string $fieldName
     * @param array $options
     * @return voild
     * @see FormHelper::input
     */
    public function input($fieldName, $options = array()) {
        $validate = null;
        if (isset($options['validate'])) {
            if ($options['validate'] === false) {
                return;
            }
            $validate = $options['validate'];
        }
        // Có thể các $fieldName ko được trùng nhau hoặc trùng thì thay đổi cách #
        $this->_maps[$fieldName] = $validate;
    }

}